from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse, JsonResponse
from django.forms.models import model_to_dict
import json
from .models import MfUser


# Create your views here.
# check_name login register
def handleCheckName(request):
    pass
    # # check_name?uname=zhangsan
    # # 解析get请求中所携带的参数
    # myName = request.GET['uname']
    # # 查询是否存在指定的记录
    # myResult = MfUser.objects.filter(uname=myName)
    # if (len(myResult) > 0):
    #     result = {"code": 0, "msg": "用户名不可用"}
    # else:
    #     result = {"code": 1, "msg": "用户名可用"}
    # # 返回结果
    # return HttpResponse(json.dumps(result))


@csrf_exempt
def handleLogin(request):
    myName = request.POST['unameOrPhone']
    myPwd = request.POST['upwd']
    myResult = MfUser.objects.filter(uname=myName, upwd=myPwd).values()
    # print(type(model_to_dict(myResult)))
    if (len(myResult) > 0):
        result = {"code": 1, "msg": "登录成功", "data": myResult[0]}
    else:
        result = {"code": 0, "msg": "用户名或者密码错误"}
    return JsonResponse(result)


def handleRegister(request):
    pass
    # # register?uname=zhangsan&upwd=123456
    # myName = request.GET['uname']
    # myPwd = request.GET['upwd']
    # user = MfUser()
    # user.uname = myName
    # user.upwd = myPwd
    # user.save()
    # result = {"code": 1, "msg": "注册成功"}
    # return HttpResponse(json.dumps(result))


def userloginpage(request):
    return render(request, 'login.html')


def userreqisterpage(request):
    return render(request, 'register.html')
